/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.flexengine.config;

import com.flexengine.logger.Logger;
import com.flexengine.utils.ResourceManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InvalidClassException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author Luann R. Athayde
 * @version 1.0
 * @since 1.0
 */
public class ConfigurationManager {

    private final String  fileName;
    private Configuration defaultConfiguration;
    
    private ConfigurationManager() {
        fileName = "configuration.flex";
        defaultConfiguration = null;
    }
    
    public boolean saveConfiguration() {
        return saveConfiguration(defaultConfiguration);
    }
    
    public boolean saveConfiguration(Configuration config) {
        File configFile = new File(ResourceManager.getLocale(fileName));
        try {
            configFile.delete();
            configFile.mkdirs();
            configFile.delete();
            if( config == null ) {
                throw new IllegalArgumentException("Configuration is NULL!!!");
            }
            configFile.createNewFile();
            try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(configFile))) {
                out.writeObject(config);
                out.flush();
            }
            //------------------------------------------------------------------
            return true;
        } catch(IllegalArgumentException | IOException e) {
            Logger.logError(getClass().getSimpleName()+"::saveConfiguration() - ERROR: "+e);
        }
        return false;
    }
    
    public Configuration loadConfiguration() {
        File configFile = new File(ResourceManager.getLocale(fileName));
        try {
            //------------------------------------------------------------------
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(configFile));
            //------------------------------------------------------------------
            Configuration config = (Configuration) in.readObject();
            //------------------------------------------------------------------
            return config;
        }
        catch(InvalidClassException ice) {
            saveConfiguration(new Configuration());
            return loadConfiguration();
        }
        catch(IOException | ClassNotFoundException e) {
            Logger.logError(getClass().getSimpleName()+"::loadConfiguration() - ERROR: "+e);
        }
        return null;
    }

    public Configuration getDefaultConfiguration() {
        return defaultConfiguration;
    }
    public void setDefaultConfiguration(Configuration defaultConfiguration) {
        this.defaultConfiguration = defaultConfiguration;
    }
    
    private static ConfigurationManager instace;
    public static ConfigurationManager getInstace() {
        if( instace == null ) {
            instace = new ConfigurationManager();
        }
        return instace;
    }
    
}
